
# 创建一个list储存着用户信息
from typing import Dict, List, Union

User_Information = [{"User_Name": "ZS", "Password" : "111111", "balance" : 23165, "Account_State" : True , "Error_Count" : 0},
                   {"User_Name": "WW", "Password": "333333", "balance" : 89454, "Account_State" : True , "Error_Count" : 0},
                   {"User_Name": "ZL", "Password": "444444", "balance" : 65565, "Account_State" : False, "Error_Count" : 3}]

# 密码错误次数
Error_Count = 0


# 登录函数
def Account_Verification(sName, sPassword):

    global User_Information
    for User_Id in range(len(User_Information)):
        # 查询用户名是否存在
        if sName == User_Information[User_Id]["User_Name"]:
            # 检测密码是否正确
            if sPassword == User_Information[User_Id]["Password"]:
                # 检测账户是否被锁定
                if User_Information[User_Id]["Account_State"] == True:
                    print("欢迎使用！")
                    return User_Id
                else:
                    print("账号已被锁定！")
                    return -2
            else:
                User_Information[User_Id]["Error_Count"] += 1
                print("密码错误,密码错误3次您的账号将被被锁定，您还有{}次机会".format(3 - User_Information[User_Id]["Error_Count"]))
                return -1

    print("账号不存在！")
    return -3

# 余额查询函数
def Account_Balance(User_Id):

    global User_Information
    print("您的余额为{}元！".format(User_Information[User_Id]["balance"]))

# 存款函数
def Deposit_Money(User_Id):

    global User_Information
    while True:
        #输入存款金额
        money = input("请输入您的存款金额：")
        #存款金额需大于零
        if float(money) <= 0:
            print("请输入大于0的金额！")
        else:
            User_Information[User_Id]["balance"] += int(money)
            print("您已存款{}元，余额为{}元！".format(money,  User_Information[User_Id]["balance"]))
            break

# 取款函数
def withdraw_Money(User_Id):

    global User_Information
    while True:
        #输入取款金额
        money = input("请输入您的取款金额：")
        #取款金额需大于零
        if float(money) <= 0:
            print("请输入大于0的金额！")
        #取款金额需小于余额
        elif float(money) > User_Information[User_Id]["balance"]:
            print("您的余额不足！")
        else:
            User_Information[User_Id]["balance"] -= float(money)
            print("您已取款{}元，余额为{}元，请核对现金！".format(money,  User_Information[User_Id]["balance"]))
            break

# 修改密码
def Change_Password(User_Id):


    global User_Information
    global Error_Count
    while True:
        # 判断密码错误次数
        if User_Information[User_Id]["Error_Count"] == 3:
            User_Information[User_Id]["Account_State"] = False
            print("账号已被锁定")
            break
        # 输入原密码
        sPassword_Old = input("请输入原密码(输入q取消)：")
        # 退出需改密码程序
        if str(sPassword_Old) == "q":
            break
        if str(sPassword_Old) != User_Information[User_Id]["Password"]:
            User_Information[User_Id]["Error_Count"] += 1
            print("密码错误！数错3次密码将被锁定！您还有{}次机会".format(3 - User_Information[User_Id]["Error_Count"]))
            continue
        # 3次修改密码机会
        for n in range(3):
            sPassword_New1 = input("请输入新密码：")
            sPassword_New2 = input("请再次输入新密码：")
            if sPassword_New1 != sPassword_New2:
                print("密码不一致！")
                print("密码不一致3次，修改密码将失败！目前错误次数{}".format(n + 1))
                continue
            User_Information[User_Id]["Password"] = str(sPassword_New2)
            print("密码修改成功！")
            break
        break

# 登陆界面
while True:
    # 输入初始界面
    print("=" * 20, "自动取款机系统", "=" * 20)
    #输入用户名密码并检验
    sUser_Name = input("请输入用户名：")
    sPass_Word = input("请输入密码：")
    User_Id = Account_Verification(sUser_Name, sPass_Word) 
    if User_Id < 0:
        continue
    # 功能选择界面
    while True:
        if User_Information[User_Id]["Account_State"] == False:
            print("账号已被锁定")
            break
        print("=" * 22, "请选择业务", "=" * 22)
        print("{0:1} {1:13} {2:15}".format(" ", "1. 余额查询", "2. 存款"))
        print("{0:1} {1:15} {2:15}".format(" ", "3. 取款", "4. 修改密码"))
        print("{0:1} {1:13}".format(" ", "5. 退出程序"))
        print("=" * 56)
        key = input("请输入任务编号：")
        #功能模块
        if key == "1":
            print("=" * 23, "余额查询", "=" * 23)
            Account_Balance(User_Id)
            input("按回车键继续")
        elif key == "2":
            print("=" * 25, "存款", "=" * 25)
            Deposit_Money(User_Id)
            input("按回车键继续")
        elif key == "3":
            print("=" * 25, "取款", "=" * 25)
            withdraw_Money(User_Id)
            input("按回车键继续")
        elif key == "4":
            print("=" * 23, "修改密码", "=" * 23)
            Change_Password(User_Id)
            input("按回车键继续")
        elif key == "5":
            print("=" * 23, "退出程序", "=" * 23)
            break
        else:
            print("=" * 23, "输入无效", "=" * 23)
            input("按回车键继续")
